package com.airnews.ticket.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Paint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.airnews.ticket.R;
import com.airnews.ticket.model.CabinItemSmall;
import com.airnews.ticket.ui.flight.FlyList;

public class FlyListViewAdapter extends SimpleAdapter  {
	LayoutInflater layoutInflater ;
	List<Map<String, Object>> items;
	Context context;

	public FlyListViewAdapter(Context context, List<Map<String, Object>> items, int resource, String[] from, int[] to) {   
		super(context, items, resource, from, to);   
		this.context = context;
		layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
	}  
	
	public FlyListViewAdapter(Context context, List<Map<String, Object>> items) { 
		super(context, items, 0, null, null);  
		this.context = context;
		this.items = items;
		this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
	}  
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		  ViewHolder holder; 
		 //得到自己定义的List的界面
		  if (convertView == null) { 
			  convertView = this.layoutInflater.inflate(R.layout.fly_row, null); 
			  holder = new ViewHolder(); 
			  holder.airline = (TextView) convertView.findViewById(R.id.airline); 
			  holder.departureTime = (TextView) convertView.findViewById(R.id.departureTime); 
			  holder.flightNo = (TextView) convertView.findViewById(R.id.flightNo); 
			  holder.arrival = (TextView) convertView.findViewById(R.id.arrival); 
			  holder.departure = (TextView) convertView.findViewById(R.id.departure); 
			  holder.airLogo = (ImageView) convertView.findViewById(R.id.airLogo); 
			  holder.baseCabin = (TextView) convertView.findViewById(R.id.baseCabin); 
			  holder.arrivalTime = (TextView) convertView.findViewById(R.id.arrivalTime); 
			  holder.price = (TextView) convertView.findViewById(R.id.price); 
			  holder.basePrice = (TextView) convertView.findViewById(R.id.basePrice); 
			  holder.cabinPrice = (TextView) convertView.findViewById(R.id.cabinPrice); 
			  holder.discount = (TextView) convertView.findViewById(R.id.discount); 
			  holder.seat = (TextView) convertView.findViewById(R.id.seat); 
			  
			  //更多的价钱项
			  holder.priceSmall = (TextView) convertView.findViewById(R.id.priceSmall); 
			  holder.basePriceSmall = (TextView) convertView.findViewById(R.id.basePriceSmall); 
			  holder.seatSmall = (TextView) convertView.findViewById(R.id.seatSmall); 
			  holder.baseCabinSmall = (TextView) convertView.findViewById(R.id.baseCabinSmall); 
			  
			  convertView.setTag(holder); 
		  } else {
			  holder = (ViewHolder) convertView.getTag(); 
		  }
		  
		  holder.airline.setText(this.items.get(position).get("airline").toString());
		  holder.departureTime.setText(this.items.get(position).get("departureTime").toString());
		  holder.flightNo.setText(this.items.get(position).get("flightNo").toString());
		  holder.arrival.setText(this.items.get(position).get("arrival").toString());
		  holder.departure.setText(this.items.get(position).get("departure").toString());
		  holder.basePrice.setText(this.items.get(position).get("basePrice").toString());
		  holder.baseCabin.setText(this.items.get(position).get("baseCabin").toString());
		  holder.arrivalTime.setText(this.items.get(position).get("arrivalTime").toString());
		  holder.price.setText(this.items.get(position).get("price").toString());
		  holder.discount.setText(this.items.get(position).get("discount").toString());
		  holder.cabinPrice.setText(this.items.get(position).get("cabinPrice").toString());
		  holder.airLogo.setImageResource(Integer.valueOf(this.items.get(position).get("airLogo").toString()));
		  
		  holder.cabinPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG );
		  holder.discount.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG );
		  
		  String seatValue = this.items.get(position).get("seat").toString();
		  if(seatValue.indexOf(">9")!=-1) {
			  holder.seat.setVisibility(View.INVISIBLE);
		  } else {
			  holder.seat.setText("票少");
			  holder.seat.setVisibility(View.VISIBLE);
		  }
		  holder.longPressLayout.setVisibility(View.GONE);
		  
		  //更多的价钱项
//		  List<CabinItemSmall> cabinItemSmallList = (List<CabinItemSmall>) this.items.get(position).get("cabinItemSmallList");
//		  if(cabinItemSmallList != null && cabinItemSmallList.size() > 0){
//			  for(CabinItemSmall cs : cabinItemSmallList){
//				  holder.priceSmall.setText(cs.getPrice());
//				  holder.baseCabinSmall.setText(cs.getBaseCabin());
//				  holder.basePriceSmall.setText(cs.getBasePrice());
//				  String seatValueSmall = cs.getSeat();
//				  if(seatValueSmall.indexOf(">9")!=-1) {
//					  holder.seatSmall.setVisibility(View.INVISIBLE);
//				  } else {
//					  holder.seatSmall.setText("票少");
//					  holder.seatSmall.setVisibility(View.VISIBLE);
//				  }
//			  }
//		  }

		  List<CabinItemSmall> cabinItemSmallList = (List<CabinItemSmall>) this.items.get(position).get("cabinItemSmallList");
		  if(cabinItemSmallList != null && cabinItemSmallList.size() > 0){
			  List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
			  for(CabinItemSmall cs : cabinItemSmallList){
				  Map<String, Object> map = new HashMap<String, Object>();
				  map.put("priceSmall", cs.getPrice());
				  map.put("baseCabinSmall", cs.getBaseCabin());
				  map.put("basePriceSmall", cs.getBasePrice());
				  map.put("seatSmall", cs.getSeat());
				  
				  Log.i("this.getPrice", cs.getPrice());
				  Log.i("this.getBaseCabin", cs.getBaseCabin());
				  Log.i("this.getBasePrice", cs.getBasePrice());
				  Log.i("this.getSeat", cs.getSeat());
				  
				  data.add(map);
			  }
			  FlyMoreListViewAdapter sa = new FlyMoreListViewAdapter(this.context, data);
			  holder.flyMorelist.setAdapter(sa);
		  }
		  
			
		  return convertView;

	}
	
	static class ViewHolder { 
		TextView airline,departureTime,flightNo; 
		ImageView airLogo;
		TextView arrival,departure,basePrice; 
		TextView baseCabin,arrivalTime,price; 
		TextView cabinPrice,discount,seat; 
		LinearLayout longPressLayout,listViewLayout;
		
		
		ListView flyMorelist;
		TextView basePriceSmall,priceSmall,cabinPriceSmall,baseCabinSmall,seatSmall; 
	} 


}
